<template>
    <div class="login">
        <van-form @submit="onSubmit" class="logm">
        <van-field
            v-model="username"
            name="username"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
            v-model="password"
            type="password"
            name="password"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px;">
            <van-button round block type="info" native-type="submit">提交</van-button>
        </div>
        </van-form>
        <router-link to="/register">
        <div class="register">
            没有账号？立即注册
        </div>
        </router-link>
    </div>
</template>



<script>
    export default {
    data() {
        return {
        username: '',
        password: '',
        };
    },
    methods: {
        onSubmit(values) {
        console.log('submit', values);
        // this.$axios.get("/api/users").then(data => {
        //     console.log(data.data);
        // })

        this.$axios.post("/api/users",values).then(data => {
            console.log(data);
            if(data.data.code == 200) {
                alert(data.data.msg)
                this.$router.push("/")
                localStorage.setItem("userInfo", JSON.stringify(values));

                 var cart = data.data.shop[0].cart;
                cart = JSON.parse(cart)
                console.log(cart);
                // if(video.video != "null") {}
                localStorage.setItem("cart", JSON.stringify(cart))
            } else {
                alert(data.data.msg)
            }

            // 获取后端传过来的值
            // console.log(data.data.shop[0].cart);
           
        })
    },
    }
    }
</script>
<style scoped>
   .register {
       margin-left: .4rem;
       color:chocolate;

   }
</style>

